//npm install --save-dev gulp
var gulp = require('gulp');
// js压缩 npm install --save-dev gulp-uglify
var uglify = require('gulp-uglify');
// 文件重命名 npm install --save-dev gulp-rename
var rename = require("gulp-rename");
// 文件合并 npm install --save-dev gulp-concat
var concat = require("gulp-concat");
// 合并css npm install gulp-clean-css --save-dev
var cleanCss = require("gulp-clean-css");
// 文件删除 npm install --save-dev gulp del
var del = require("del");
// 文件版本 npm install gulp-rev-append --save-dev
var rev = require("gulp-rev-append");
// 筛选文件 npm install --save-dev gulp-filter
var filter = require('gulp-filter');
// js校验 npm install jshint gulp-jshint --save-dev
var jshint = require("gulp-jshint");
// 多个文件来源 npm install --save-dev gulp merge-stream
var merge = require('merge-stream');
// 替换 npm install --save-dev gulp-replace
var replace = require("gulp-replace");
// 监听更改的文件 npm install --save-dev gulp-watch
var watch = require("gulp-watch");
// html压缩 npm install --save gulp-htmlmin
var htmlmin = require("gulp-htmlmin");
// 代码检查 npm install --save-dev gulp-babel babel-core babel-preset-env
var babel = require("gulp-babel");
// 爬取需要的文字文件进行压缩 npm install gulp-font-spider --save-dev
var gulpfontspider = require("gulp-font-spider");

// html 模块合并 
var fileinclude = require('gulp-file-include');
gulp.task('fileinclude', function () {
  gulp.src(['./src/index.html',
      './src/wallet.html', './src/deposits.html', './src/withdraw.html', './src/transfer.html', './src/google2.html', './src/changepw.html', './src/untiegoogle2.html',
      './src/auth.html', './src/authfail.html', './src/changepw.html', './src/verified.html', './src/uidchange.html',
      './src/transactions.html',
      './src/shop-sininfo.html', './src/shop-orderlist.html', './src/shop-virorderinfo.html', './src/shop-finorderinfo.html', './src/shop-finlist.html',
      './src/user_setting.html', './src/user_forgetpw.html', './src/application-back.html',
      './src/refund-processing.html', './src/return-logistics.html', './src/refund-failure.html', './src/application-history.html',
      './src/article-list.html', './src/article-info.html', './src/404.html', './src/service_text.html', './src/shop-grid.html', './src/address-list.html', './src/notifications-list.html', './src/order-logistics.html', './src/common_question.html',
      './src/email.html', './src/login_new.html', './src/register_new.html', './src/email_send_code.html', './src/password_reset.html', './src/google_new.html',
      './src/system.html'
    ])
    .pipe(fileinclude({
      prefix: '@@',
      basepath: '@root',
      context: {
        name: 'test'
      }
    }))
    .pipe(rev())
    .pipe(gulp.dest('./dist'));
});

// 压缩js
gulp.task('build-js', ['common-js'], function (cb) {
  return gulp.src(['dist/js/*.js', 'dist/js/user/*.js', 'dist/js/shop/*.js', 'dist/js/setting/*.js', '!dist/js/plugins.js', '!dist/js/active.js'])
    .pipe(uglify())
    .pipe(rename({
      extname: '.min.js'
    }))
    .pipe(gulp.dest('build/js'));
});

// 合并js

gulp.task('common-js', function () {
  var path = 'build/js';
  var name = 'common';
  return gulp.src(['dist/js/plugins.js', 'dist/js/active.js', 'dist/js/bcat_api.js'])
    .pipe(concat(name + '.js'))
    .pipe(gulp.dest(path))
    .pipe(uglify())
    .pipe(rename(name + '.min.js'))
    .pipe(gulp.dest(path));
});
// 合并css
gulp.task('build-css', function () {
  var name = "common";
  return gulp.src([
      'dist/css/style.css',
      'dist/css/shop-grid.css',
      'dist/css/plugins/themify-icons.css',
      'dist/css/plugins/mainmenu.css',
      'dist/iconfonts/iconfont.css',
      'dist/css/custom.css',
      'dist/css/verify.css'
    ])
    .pipe(concat(name + ".css"))
    .pipe(gulp.dest('build/css'))
    .pipe(cleanCss())
    .pipe(rename(name + '.min.css'))
    .pipe(gulp.dest('build/css'))
});

// 压缩css
gulp.task('css', function () {
  var needcss = gulp.src([
      'dist/css/shop-sininfo.css'
    ])
    .pipe(gulp.dest('build/css'))
    .pipe(cleanCss())
    .pipe(rename('shop-sininfo.min.css'))
    .pipe(gulp.dest('build/css'))
  var fdjcss = gulp.src([
      'dist/css/product_fangdajing.css'
    ])
    .pipe(gulp.dest('build/css'))
    .pipe(cleanCss())
    .pipe(rename('product_fangdajing.min.css'))
    .pipe(gulp.dest('build/css'))
  var name = "common";
  var commoncss = gulp.src([
      'dist/css/style.css',
      'dist/css/shop-grid.css',
      'dist/css/plugins/mainmenu.css',
      'dist/css/custom.css'
    ])
    .pipe(concat(name + ".css"))
    .pipe(gulp.dest('build/css'))
    .pipe(cleanCss())
    .pipe(rename(name + '.min.css'))
    .pipe(gulp.dest('build/css'));
  var fontscss = gulp.src([
      'dist/css/icomoon.css',
      // 'dist/iconfonts/iconfont.css',
      // 'dist/css/plugins/themify-icons.css',
    ])
    .pipe(concat("fonts.css"))
    .pipe(gulp.dest('build/css'))
    .pipe(cleanCss())
    .pipe(rename('fonts.min.css'))
    .pipe(gulp.dest('build/css'));;
  var layuicss = gulp.src(['dist/css/layui.css'])
    .pipe(gulp.dest('build/css'));
  var layuicssmodules = gulp.src(['dist/css/modules/**'])
    .pipe(gulp.dest('build/css/modules'));
  return merge(needcss, commoncss, fontscss, layuicss, fdjcss, layuicssmodules);
});
// 清空dist
gulp.task('clean:dist', function (cb) {
  del([
    // 这里我们使用一个通配模式来匹配 `mobile` 文件夹中的所有东西
    'dist/**/*',
  ], cb);
});

// 清空dist
gulp.task('clean:build', function (cb) {
  return del([
    // 这里我们使用一个通配模式来匹配 `mobile` 文件夹中的所有东西
    'build/**/*',
  ], cb);
});

// fonts
gulp.task('fonts', function () {
  return gulp.src([
      // 'dist/fonts/themify.eot', 
      // 'dist/fonts/themify.svg', 
      // 'dist/fonts/themify.ttf', 
      // 'dist/fonts/themify.woff', 
      'dist/iconfonts/iconfont.eot',
      'dist/iconfonts/iconfont.svg',
      'dist/iconfonts/iconfont.ttf',
      'dist/iconfonts/iconfont.woff',
      'dist/fonts/icomoon.eot',
      'dist/fonts/icomoon.svg',
      'dist/fonts/icomoon.ttf',
      'dist/fonts/icomoon.woff',
    ])
    .pipe(gulp.dest('build/fonts'));
});
// layui
gulp.task('js', function () {
  var layui = gulp.src(['dist/layui.js'])
    .pipe(gulp.dest('build/'));
  var lib = gulp.src(['dist/js/lib/*.js'])
    .pipe(gulp.dest('build/js/lib'));
  var lay = gulp.src(['dist/lay/modules/*.js', ])
    .pipe(gulp.dest('build/lay/modules'));
  var uglifyjs = gulp.src(['dist/js/*.js', 'dist/js/user/*.js', 'dist/js/shop/*.js', 'dist/js/setting/*.js', '!dist/js/plugins.js', '!dist/js/active.js', '!dist/js/bcat_api.js', '!dist/js/verify.min.js','!dist/lib/resize.min.js'])
    .pipe(babel())
    .pipe(uglify())
    .pipe(rename({
      extname: '.min.js'
    }))
    .pipe(gulp.dest('build/js'));

  var path = 'build/js';
  var name = 'common';
  var commonjs = gulp.src(['dist/js/plugins.js', 'dist/js/active.js', 'dist/js/bcat_api.js'])
    .pipe(concat(name + '.js'))
    .pipe(gulp.dest(path))
    .pipe(babel())
    .pipe(uglify())
    .pipe(rename(name + '.min.js'))
    .pipe(gulp.dest(path));
  return merge(layui,lib, lay, uglifyjs, commonjs);

});



// 生产版本
gulp.task('build-html', function () {
  return gulp.src(['./src/index.html',
      './src/wallet.html', './src/deposits.html', './src/withdraw.html', './src/transfer.html', './src/google2.html', './src/changepw.html', './src/untiegoogle2.html',
      './src/auth.html', './src/authfail.html', './src/changepw.html', './src/verified.html', './src/uidchange.html',
      './src/transactions.html',
      './src/shop-sininfo.html', './src/shop-orderlist.html', './src/shop-virorderinfo.html', './src/shop-finorderinfo.html', './src/shop-finlist.html',
      './src/user_setting.html', './src/user_forgetpw.html', './src/application-back.html',
      './src/refund-processing.html', './src/return-logistics.html', './src/refund-failure.html', './src/application-history.html',
      './src/article-list.html', './src/article-info.html', './src/404.html', './src/service_text.html', './src/shop-grid.html', './src/address-list.html', './src/notifications-list.html', './src/order-logistics.html', './src/common_question.html',
      './src/email.html', './src/login_new.html', './src/register_new.html', './src/email_send_code.html', './src/password_reset.html', './src/google_new.html',
      './src/system.html'
    ])
    .pipe(fileinclude({
      prefix: '@@',
      basepath: '@root',
      context: {
        name: 'build'
      }
    }))
    .pipe(rev())
    .pipe(replace('../build/', ''))
    .pipe(htmlmin({
      collapseWhitespace: true, // 清除空格
      removeComments: true, // 清除HTML注释
      minifyJS: true
    }))
    .pipe(gulp.dest('build'));
});
// 图片处理
gulp.task('img', function () {
  return gulp.src(['dist/images/**/*.{jpg,jpeg,png,gif,ico}'])
    .pipe(gulp.dest('build/images'));
});

// script
gulp.task('script', function () {
  return gulp.src(['src/js/**/*.js'])
    // .pipe(gulp.dest('build/js'))
    .pipe(filter(['**/*', '!**/*.min.js'])) // 筛选出管道中的非 *.min.js 文件
    .pipe(jshint('.jshintrc')) // js的校验与合并，根据需要开启
    // .pipe(jshint.reporter('default'))
    // .pipe(concat('main.js'))
    // .pipe(gulp.dest('dist/js'))
    .pipe(rename({
      suffix: '.min'
    }))
    .pipe(uglify())
    .pipe(gulp.dest('build/js'))
});

// watch 开启本地服务器并监听
gulp.task('watch', function () {


  // 监控 CSS 文件，有变动则执行CSS注入
  gulp.watch('dist/css/**/*.css', ['css']);
  // 监控 js 文件，有变动则执行 script 任务
  gulp.watch('dist/js/**/*.js', ['js']);
  // 监控图片文件，有变动则执行 image 任务
  gulp.watch('dist/images/**/*', ['img']);
  // 监控 html 文件，有变动则执行 html 任务
  gulp.watch('dist/**/*.html', ['html']);

});

gulp.task('build', ['js', 'css', 'img', 'fonts'], function () {
  gulp.start('build-html');
});
// 默认任务，依赖清空任务
gulp.task('test', ['clean:build'], function () {
  gulp.start('build');
});

gulp.task('default', ['fileinclude']);